package com.hzhk.happyrent.ui.activity.order;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hzhk.happyrent.R;
import com.hzhk.happyrent.bean.CodeModel;
import com.hzhk.happyrent.bean.order.OrderDetailModel;
import com.hzhk.happyrent.network.API;
import com.hzhk.happyrent.network.HttpManager;
import com.hzhk.happyrent.network.MyStringResponse;
import com.hzhk.happyrent.ui.OrderCancelDialog;
import com.hzhk.happyrent.ui.activity.AgreementActivity;
import com.hzhk.happyrent.ui.activity.PayPeriodsActivity;
import com.hzhk.happyrent.utils.MyPerference;
import com.hzhk.happyrent.utils.TimeUtil;
import com.tamic.novate.Throwable;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class OrderDetailActivity extends AppCompatActivity {
    @BindView(R.id.activity_titlebar_left)
    ImageView activityTitlebarLeft;
    @BindView(R.id.activity_titlebar_center)
    TextView activityTitlebarCenter;
    @BindView(R.id.product_name)
    TextView productName;
    @BindView(R.id.order_state)
    TextView orderState;
    @BindView(R.id.order_info)
    TextView orderInfo;
    @BindView(R.id.order_brand)
    TextView orderBrand;
    @BindView(R.id.order_specification)
    TextView orderSpecification;
    @BindView(R.id.order_rent_pay)
    TextView orderRentPay;
    @BindView(R.id.order_rent_contract_value)
    TextView orderRentContractValue;
    @BindView(R.id.order_more)
    ImageView orderMore;
    @BindView(R.id.order_detail_tab)
    TabLayout orderDetailTab;
    @BindView(R.id.item_view_add)
    LinearLayout itemViewAdd;
    @BindView(R.id.order_cancel)
    Button orderCancel;
    @BindView(R.id.order_pay)
    Button orderPay;
    @BindView(R.id.more_show_or_hide)
    LinearLayout moreShowOrHide;
    @BindView(R.id.order_hey1)
    TextView orderHey1;
    @BindView(R.id.order_value1)
    TextView orderValue1;
    @BindView(R.id.order_hey2)
    TextView orderHey2;
    @BindView(R.id.order_value2)
    TextView orderValue2;
    @BindView(R.id.order_hey3)
    TextView orderHey3;
    @BindView(R.id.order_value3)
    TextView orderValue3;
    @BindView(R.id.order_hey4)
    TextView orderHey4;
    @BindView(R.id.order_value4)
    TextView orderValue4;
    @BindView(R.id.order_hey5)
    TextView orderHey5;
    @BindView(R.id.order_value5)
    TextView orderValue5;
    @BindView(R.id.order_hey6)
    TextView orderHey6;
    @BindView(R.id.order_value6)
    TextView orderValue6;
    @BindView(R.id.order_hey7)
    TextView orderHey7;
    @BindView(R.id.order_value7)
    TextView orderValue7;
    @BindView(R.id.order_hey8)
    TextView orderHey8;
    @BindView(R.id.order_value8)
    TextView orderValue8;
    @BindView(R.id.order_hey9)
    TextView orderHey9;
    @BindView(R.id.order_value9)
    TextView orderValue9;
    @BindView(R.id.bt_odd_copy)
    TextView btOddCopy;
    private OrderCancelDialog orderCancelDialog;
    private String order_state;
    private static OrderDetailModel orderDetailModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_detail);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        activityTitlebarCenter.setText("订单详情");
        initData(getIntent().getStringExtra("order_id"));
        orderMore.setSelected(false);
        order_state = getIntent().getStringExtra("order_state");
        if (!TextUtils.isEmpty(order_state)) {
            orderState.setText(order_state);
        }
        switch (order_state) {
            case "待支付":
            case "待确认":
            case "已取消":
                orderDetailTab.addTab(orderDetailTab.newTab().setText("租赁信息"));
                break;
            case "已完成":
                orderDetailTab.addTab(orderDetailTab.newTab().setText("归还信息"));
                orderDetailTab.addTab(orderDetailTab.newTab().setText("账期信息"));
                orderDetailTab.addTab(orderDetailTab.newTab().setText("租赁信息"));
                break;
            case "发货中":
                btOddCopy.setVisibility(View.VISIBLE);
                orderDetailTab.addTab(orderDetailTab.newTab().setText("物流信息"));
                orderDetailTab.addTab(orderDetailTab.newTab().setText("账期信息"));
                orderDetailTab.addTab(orderDetailTab.newTab().setText("租赁信息"));
                break;
            case "使用中":
            case "待发货":
                orderDetailTab.addTab(orderDetailTab.newTab().setText("账期信息"));
                orderDetailTab.addTab(orderDetailTab.newTab().setText("租赁信息"));
                break;
        }
        orderPay.setVisibility(View.GONE);
        orderCancel.setVisibility(View.GONE);

//        switch (order_state) {
//            case "待支付":
//                break;
//            case "待确认":
//                orderPay.setVisibility(View.GONE);
//                orderCancel.setVisibility(View.GONE);
//                break;
//            case "已取消":
//                orderPay.setVisibility(View.GONE);
//                orderCancel.setVisibility(View.GONE);
//                break;
//            case "已完成":
//                orderPay.setVisibility(View.GONE);
//                orderCancel.setVisibility(View.GONE);
//                break;
//            case "待发货":
//            case "发货中":
//            case "使用中":
//                orderCancel.setText("延迟租期");
//                orderCancel.setTextColor(getResources().getColor(R.color.tab_red_80));
//                orderCancel.setBackground(getResources().getDrawable(R.drawable.bg_red_fillet));
//                orderPay.setText("预约还机");
//                orderPay.setVisibility(View.GONE);
//                orderCancel.setVisibility(View.GONE);
//                break;
//        }
//        orderDetailTab.addTab(orderDetailTab.newTab().setText("续租信息"));
//        orderDetailTab.addTab(orderDetailTab.newTab().setText("账期信息"));
        orderDetailTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                loadViewRent(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void initData(String order_id) {
        Map<String, Object> params = new HashMap<>();
        params.put("user_id", MyPerference.getInstance().getUserId());
        params.put("order_id", order_id);
        HttpManager.getInstance().get(API.ORDER_DETAIL, params, new MyStringResponse(this) {
            @Override
            public void onError(Object tag, Throwable e) {

            }

            @Override
            public void onCancel(Object tag, Throwable e) {

            }

            @Override
            public void onNext(Object tag, String response) {
                CodeModel codeModel = new Gson().fromJson(response, CodeModel.class);
                if (0 == codeModel.getErrCode()) {
                    orderDetailModel = new Gson().fromJson(response, OrderDetailModel.class);
                    loadViewRent(0);
                    productName.setText(orderDetailModel.getData().getGoodsInfo().getGoodsName());
                    orderInfo.setText(orderDetailModel.getData().getGoodsInfo().getGoodsSubTitle());
                    orderBrand.setText(orderDetailModel.getData().getGoodsInfo().getBrandName());
                    String substring = orderDetailModel.getData().getGoodsSku().getAttrKeyPath().substring(1, orderDetailModel.getData().getGoodsSku().getAttrKeyPath().length() - 1);
                    orderSpecification.setText(substring.replace("][", "/"));
                    orderRentPay.setText("￥" + orderDetailModel.getData().getPrice() + "/月");
                    orderRentContractValue.setText("￥" + orderDetailModel.getData().getRealGoodsPrice());
                }
            }
        });
    }

    @OnClick({R.id.activity_titlebar_left, R.id.order_more, R.id.order_cancel, R.id.order_pay, R.id.order_value6
            , R.id.bt_odd_copy})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.activity_titlebar_left:
                finish();
                break;
            case R.id.order_value6:
                Intent intent = new Intent(OrderDetailActivity.this, AgreementActivity.class);
                intent.putExtra("name", "用户租赁及服务协议");
                intent.putExtra("id", "2");
                startActivity(intent);
                break;
            case R.id.order_more:
                if (orderMore.isSelected()) {
                    orderMore.setSelected(false);
                    moreShowOrHide.setVisibility(View.GONE);
                } else {
                    orderMore.setSelected(true);
                    moreShowOrHide.setVisibility(View.VISIBLE);
                }
                break;
            case R.id.order_cancel:
                switch (order_state) {
                    case "待支付":
                    case "待确认":
                        cancelDialog();
                        break;
                    case "已取消":
                        break;
                    case "已完成":
                        break;
                    case "待发货":
                    case "发货中":
                    case "使用中":
                        break;
                }
                break;
            case R.id.order_pay:
                /*Intent intent = new Intent(this, PreOrderActivity.class);
                intent.putExtra(Constants.ORDER_ID,getIntent().getStringExtra("order_id"));
                intent.putExtra(Constants.ORDER_ADDRESS,orderDetailModel.getData().getAddress());
                intent.putExtra(Constants.TOTAL_PRICE,orderDetailModel.getData().getTotalPrice());
                intent.putExtra(Constants.ATTR_KEY_PATH,orderDetailModel.getData().getGoodsSku().getAttrKeyPath());
                intent.putExtra(Constants.GOODS_NAME,orderDetailModel.getData().getGoodsInfo().getGoodsName());
                intent.putExtra(Constants.GOODS_IMAGE,orderDetailModel.getData().getGoodsInfo().getGoodsImage());
                intent.putExtra(Constants.CONSIGNEE,orderDetailModel.getData().getConsignee());
                intent.putExtra(Constants.MOBILE,orderDetailModel.getData().getMobile());
                startActivity(intent);*/
                switch (order_state) {
                    case "待支付":
                        Intent i = new Intent(this, PayPeriodsActivity.class);
                        i.putExtra("pay_total", orderDetailModel.getData().getWxPayPrice());
                        i.putExtra("order_id", orderDetailModel.getData().getId());
                        i.putExtra("come_from", "order_list");
                        startActivity(i);
                        break;
                    case "待确认":
                        break;
                    case "已取消":
                        break;
                    case "已完成":
                        break;
                    case "待发货":
                    case "发货中":
                        break;
                    case "使用中":
                        //startActivity(new Intent(this,PreOrderActivity.class));
                        break;
                }
                break;
            case R.id.bt_odd_copy:
                if(!TextUtils.isEmpty(orderValue3.getText().toString())){
                    copy(orderValue3.getText().toString());
                }
                break;
        }
    }
    public void copy(String content) {
        // 得到剪贴板管理器
        ClipboardManager cmb = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        cmb.setText(content.trim());
        Toast.makeText(this, "运单号已复制", Toast.LENGTH_SHORT).show();
    }

    private void cancelDialog() {
        orderCancelDialog = new OrderCancelDialog(this, getIntent().getStringExtra("order_id"));
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (orderCancelDialog != null && orderCancelDialog.isShowing()) {
            orderCancelDialog.closeCancel();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void loadViewRent(int position) {
        switch (order_state) {
            case "待支付":
            case "待确认":
            case "已取消":
                initText(3);
                break;
            case "已完成":
                if (position == 0) {
                    initText(4);

//                    inflater(R.layout.item_order_detail_payment);
                } else if (position == 1) {
                    initText(2);
//                    new ViewHolder(inflater(R.layout.item_order_detail_rent_info));
                } else if (position == 2) {
//                    new ViewHolder(inflater(R.layout.item_order_detail_rent_info));
                    initText(3);
                }
                break;
            case "发货中":
                if (position == 0) {
                        initText(5);
                } else if (position == 1) {
                        initText(2);
                } else if (position == 2) {
                        initText(3);
                }
                break;
            case "待发货":
            case "使用中":
                if (position == 0) {
                    initText(2);
                } else if (position == 1) {
                    initText(3);
                }
                break;
        }
    }

    private void initText(int i) {
        String[] strings;
        switch (i) {
            case 2:
                strings = getResources().getStringArray(R.array.bill_info);
                orderHey1.setText(strings[0]);
                orderHey2.setText(strings[1]);
                orderHey3.setText(strings[2]);
                orderHey4.setText(strings[3]);
                orderHey5.setText(strings[4]);
                orderHey6.setText(strings[5]);
                orderHey7.setText(strings[6]);
                orderHey8.setText(strings[7]);
                orderHey9.setText(strings[8]);
                orderHey4.setVisibility(View.VISIBLE);
                orderValue4.setVisibility(View.VISIBLE);
                orderHey5.setVisibility(View.VISIBLE);
                orderValue5.setVisibility(View.VISIBLE);
                orderValue1.setText(orderDetailModel.getData().getPayType());
                orderValue2.setText(orderDetailModel.getData().getZuDate() + "期");
                orderValue3.setText("￥" + orderDetailModel.getData().getZuMoneyTotal());
                orderValue4.setText("￥" + orderDetailModel.getData().getZuRealMoney());
                orderValue5.setText("￥" + orderDetailModel.getData().getWxPayPrice());
                orderValue6.setClickable(false);
                orderValue6.setTextColor(getResources().getColor(R.color.black_60));
                orderValue6.setText("￥" + orderDetailModel.getData().getZuNoPayMoney());
                orderValue7.setText(TimeUtil.getStrTime(orderDetailModel.getData().getPayDate()));
                orderValue8.setText("");
                orderValue9.setText("");
                break;
            case 3:
                strings = getResources().getStringArray(R.array.arr_order_info);
                orderHey1.setText(strings[0]);
                orderHey2.setText(strings[1]);
                orderHey3.setText(strings[2]);
                orderHey5.setText(strings[4]);
                orderHey6.setText(strings[5]);

                orderValue1.setText(orderDetailModel.getData().getOrderCode());
                orderValue2.setText(orderDetailModel.getData().getZuDate() + "期");
                orderValue3.setText(TimeUtil.getStrTime(orderDetailModel.getData().getCreateDate()));
                if (!TextUtils.isEmpty(orderDetailModel.getData().getStartZuTime()) && !"0".equals(orderDetailModel.getData().getStartZuTime())) {
                    orderHey4.setVisibility(View.VISIBLE);
                    orderValue4.setVisibility(View.VISIBLE);
                    orderHey4.setText(strings[3]);
                    orderValue4.setText(TimeUtil.getStrTime(orderDetailModel.getData().getStartZuTime()));
                } else {
                    orderHey4.setVisibility(View.GONE);
                    orderValue4.setVisibility(View.GONE);
                }
                if (!TextUtils.isEmpty(orderDetailModel.getData().getEndZuTime())) {
                    orderHey5.setVisibility(View.VISIBLE);
                    orderValue5.setVisibility(View.VISIBLE);
                    orderHey5.setText(strings[4]);
                    orderValue5.setText(TimeUtil.getStrTime(orderDetailModel.getData().getEndZuTime()));
                } else {
                    orderHey5.setVisibility(View.GONE);
                    orderValue5.setVisibility(View.GONE);
                }
                orderValue6.setTextColor(getResources().getColor(R.color.tab_red_80));
                orderValue6.setText("《用户租赁及服务协议》");
                orderValue6.setClickable(true);
                if (!TextUtils.isEmpty(orderDetailModel.getData().getConsignee())) {
                    orderHey7.setText(strings[6]);
                    orderHey8.setText(strings[7]);
                    orderHey9.setText(strings[8]);
                    orderValue7.setText(orderDetailModel.getData().getConsignee());
                    orderValue8.setText(orderDetailModel.getData().getMobile());
                    orderValue9.setText(orderDetailModel.getData().getAddress());
                } else {
                    orderHey7.setText("");
                    orderHey8.setText("");
                    orderHey9.setText("");
                    orderValue7.setText("");
                    orderValue8.setText("");
                    orderValue9.setText("");
                }
                break;
            case 4:
                strings = getResources().getStringArray(R.array.return_info);
                orderHey1.setText(strings[0]);
                orderHey2.setText(strings[1]);
                orderHey3.setText(strings[2]);
                orderHey4.setText(strings[3]);
                orderHey5.setText(strings[4]);
                orderHey6.setText(strings[5]);
                orderHey7.setText(strings[6]);
                orderHey8.setText(strings[7]);
                orderHey9.setText(strings[8]);
                orderHey4.setVisibility(View.VISIBLE);
                orderValue4.setVisibility(View.VISIBLE);
                orderHey5.setVisibility(View.VISIBLE);
                orderValue5.setVisibility(View.VISIBLE);
                orderValue1.setText(TimeUtil.getStrTime(orderDetailModel.getData().getGoodsReturnInfo().getCreateTime()));
                if (!TextUtils.isEmpty(orderDetailModel.getData().getGoodsReturnInfo().getCarriageNumber())) {
                    orderValue2.setText(orderDetailModel.getData().getGoodsReturnInfo().getCarriageNumber());
                } else {
                    orderValue2.setText("");
                }
                orderValue3.setText("");
                orderValue4.setText("");
                orderValue5.setText("");
                orderValue6.setText("");
                orderValue7.setText("");
                orderValue8.setText("");
                orderValue9.setText("");
                break;
            case 5:
                strings = getResources().getStringArray(R.array.logistics_info);
                orderHey1.setText(strings[0]);
                orderHey2.setText(strings[1]);
                orderHey3.setText(strings[2]);
                orderHey4.setText(strings[3]);
                orderHey5.setText(strings[4]);
                orderHey6.setText(strings[5]);
                orderHey7.setText(strings[6]);
                orderHey8.setText(strings[7]);
                orderHey9.setText(strings[8]);
                orderHey4.setVisibility(View.VISIBLE);
                orderValue4.setVisibility(View.VISIBLE);
                orderHey5.setVisibility(View.VISIBLE);
                orderValue5.setVisibility(View.VISIBLE);
                orderValue1.setText(TimeUtil.getStrTime(orderDetailModel.getData().getCarriageTime()));
                if (!TextUtils.isEmpty(orderDetailModel.getData().getCarriageNumber())) {
                    orderValue2.setText(orderDetailModel.getData().getCarriageCompany());
                    orderValue3.setText(orderDetailModel.getData().getCarriageNumber());
                } else {
                    orderValue2.setText("");
                    orderValue3.setText("");
                }
                orderValue4.setText("");
                orderValue5.setText("");
                orderValue6.setText("");
                orderValue7.setText("");
                orderValue8.setText("");
                orderValue9.setText("");
                break;
        }
    }
}
